<?php
	require_once(dirname(__FILE__) . '/../fpdf17/fpdf.php');
	require_once (dirname(__FILE__) . '/converter.php');
	
	//http://www.fpdf.org/en/doc/fpdf.htm
		
	function createImageFile($uploadDir, $width, $height, $scale, $img, $type, $dpi_x, $dpi_y){
		$widthInch = $width / $dpi_x;
		$widthCm = inchToCm($widthInch);
		$width = $widthCm;
		
		$heightInch = $height /$dpi_y;
		$heightCm = inchToCm($heightInch);
		$height = $heightCm;

		$img = str_replace('data:image/' . $type .';base64,', '', $img);
		$img = str_replace(' ', '+', $img);
		$data = base64_decode($img);
		
		// Update DPI information in the JPG header
		$data[13] = chr(1);
		$data[14] = chr(floor($dpi_x/255));
		$data[15] = chr($dpi_x%255);
		$data[16] = chr(floor($dpi_y/255));
		$data[17] = chr($dpi_y%255);
		
		$imageFile = $uploadDir . uniqid() . "." . $type;
		$success = file_put_contents($imageFile, $data);
		//print $success ? $imageFile : 'Unable to save the file.';
		
		if($success){
			//$pdf = new FPDF('P','mm',array(100,150)); Example with a custom 100x150 mm page size:
			$pdf = new FPDF(getOrienation($width, $height),'cm',array($width,$height));
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',16);
			//$pdf->Cell(40,10,'Hello World!');
			// Insert a logo in the top-left corner at 300 dpi
			//$pdf->Image($imageFile,0,0,-300); //x - y : both 0
			$pdf->Image($imageFile,0,0,$width,$height,0,$type);
			
			unlink($imageFile);
			return $pdf;
		} else {
			throw new Exception("Image file couldn't be created!");
		}
	}
	
	function getOrienation($width, $height){
		if($height > $width){
			return "P";
		} else {
			return "L";
		}
	}
?>